Login = {
	loginDivs:"",
	logInByEnter:function(e){
		if(e.keyCode == 13){
			Login.logIn();
		}
	},
	logIn:function(){
		if(Valid.validate(jQuery("div#login-logIn"))){
			jQuery.ajax({
				url:Fundr.ajaxURL("loginService","loginTraditional"),
				type:"POST",
				data:Fundr.getAjaxData(jQuery('div#login-logIn')),
				success:function(data){
					if(data != -1 && data.length == 17){
						Login.showLoggedIn();
						Login.getUserByID(data);
						jQuery(document).trigger('loggedin');
					}
					Alert.getSystemMessages();
				}
			});
		}
	},
	logOut:function(){
		jQuery.ajax({
			url:Fundr.ajaxURL("loginController","logOut"),
			type:"POST",
			success:function(data){
				location.reload();
			}
		});
	},
	getUserByID:function(encryptedUserID){
		jQuery.ajax({
			url:Fundr.ajaxURL("userService","getUserByID"),
			type:"POST",
			data:{
				userID:encryptedUserID
			},
			success:function(data){
				var name = data.DATA[0][1] + " " + data.DATA[0][3];
				jQuery('#userFullName').text(name);
			}
		});
	},
	showLoggedIn:function(){
		Login.loginDivs.hide();
		jQuery('#login-loggedIn').show();
		jQuery('#login-loggedInBottom').show();
	},
	showLoginForm:function(){
		Login.loginDivs.hide();
		jQuery('#login-logIn').show();
	},
	showCreateForm:function(){
		Login.loginDivs.hide();
		jQuery('#login-newUser').show();
		jQuery('#login-newUserBottom').show();
	},
	createUser:function(){
		if(Valid.validate(jQuery("div#login-newUser"))){
			var args = Fundr.getAjaxData(jQuery('div#login-newUser'));
			jQuery.ajax({
				url:Fundr.ajaxURL("userService","createAccount"),
				type:"POST",
				data:args,
				success:function(data){
					if(data != -1 && data.length == 17){
						jQuery('input[name=email]').val(args.email);
						jQuery('input[name=password]').val(args.password);
						Login.logIn();
					} else {
						Alert.getSystemMessages();
					}
				}
			});
		}
	},
	loginKeyUp:function(t,e){
		if(e.keyCode == 13){
			Login.createUser();
		}
	}
};
jQuery(document).ready(function(){
	Login.loginDivs = jQuery('div.loginDiv');
});